﻿#include "widget_charts.h"
#include "ui_widget_charts.h"

#include <QtWidgets>
#include "net_data.h"
#include "udp_master.h"
#include "net_mgr.h"

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif


WidgetCharts::WidgetCharts(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::WidgetCharts)
{
	ui->setupUi(this);


	//setFrameShape( QFrame::Box );
	//setLineWidth(1);
	//setMidLineWidth(0);// # 如果需要特殊效果，可以设置mid - line width
	setStyleSheet("border-color: black;");;

	ui->widget_1->yAxis->grid()->setSubGridVisible(false);
	ui->widget_1->xAxis->grid()->setVisible(false);

	ui->widget_2->yAxis->grid()->setSubGridVisible(false);
	ui->widget_2->xAxis->grid()->setVisible(false);

	ui->widget_3->yAxis->grid()->setSubGridVisible(false);
	ui->widget_3->xAxis->grid()->setVisible(false);

	ui->widget_4->yAxis->grid()->setSubGridVisible(false);
	ui->widget_4->xAxis->grid()->setVisible(false);

	ui->widget_5->yAxis->grid()->setSubGridVisible(false);
	ui->widget_5->xAxis->grid()->setVisible(false);

	ui->widget_6->yAxis->grid()->setSubGridVisible(false);
	ui->widget_6->xAxis->grid()->setVisible(false);

	ui->widget_7->yAxis->grid()->setSubGridVisible(false);
	ui->widget_7->xAxis->grid()->setVisible(false);

	ui->widget_8->yAxis->grid()->setSubGridVisible(false);
	ui->widget_8->xAxis->grid()->setVisible(false);

	ui->checkBox->setStyleSheet("QCheckBox::indicator{subcontrol-position:center;}");
	ui->checkBox_2->setStyleSheet("QCheckBox::indicator{subcontrol-position:center; }");
	ui->checkBox_3->setStyleSheet("QCheckBox::indicator{subcontrol-position:center; }");
	ui->checkBox_4->setStyleSheet("QCheckBox::indicator{subcontrol-position:center;}");


	init_graph(ui->widget_1, 60);
	init_graph(ui->widget_2, 60);
	init_graph(ui->widget_3, 60);
	init_graph(ui->widget_4, 60);
	init_graph(ui->widget_5, 60);
	init_graph(ui->widget_6, 60);
	init_graph(ui->widget_7, 60);
	init_graph(ui->widget_8, 60);
    
    
    connect(NetMgr::upd_server, &UDPMaster::sigNewData, [=]
		{
            update_graph(ui->widget_1, 13);
            update_graph(ui->widget_2, 0);
            update_graph(ui->widget_3, 1);
            update_graph(ui->widget_4, 12);
            update_graph(ui->widget_5, 15);
            update_graph(ui->widget_6, 2);
            update_graph(ui->widget_7, 3);
            update_graph(ui->widget_8, 14);
		});
}

WidgetCharts::~WidgetCharts()
{
	delete ui;
}


void WidgetCharts::init_graph(CustomPlot* widget, int index)
{
	int graphCount = widget->graphCount();

//	if (graphCount == 0)
		//	widget->addGraph();
    //	widget->AddNewPlot("", QVector<double>(), QVector<double>());

	widget->xAxis->setRange(0, 100);
	widget->yAxis->setRange(-100, 100);
	//widget->xAxis->sett
	widget->replot();
}

void WidgetCharts::update_graph(CustomPlot* widget, int index)
{
	QVector<LowerNode*>& lower_data = NetData::Instance()->lower_data;
	const	LowerNode* lower_0 = lower_data.at(index);
	int cnt = lower_0->Value.count();

   if (widget->graphCount() == 0)
         widget->AddNewPlot("", QVector<double>(), QVector<double>());

	widget->graph(0)->addData((double)cnt, lower_0->Value.last());
	int x_start = cnt > 100 ? cnt - 100 : 0;
	//widget->rescaleAxes();
	widget->xAxis->setRange(x_start, x_start + 100);
	widget->replot();
}
